<template>
  <div class="wHd">
    <div class="pathWrap" >
      <el-breadcrumb separator-class="el-icon-arrow-right" class="app-breadcrumb" separator="/">
        <span style="float: left">您现在的位置：</span>
        <transition-group name="breadcrumb">
          <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
              <span v-if="item.redirect==='noRedirect'||index==levelList.length-1" class="no-redirect"
              >{{ item.meta.title }}</span>
            <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
          </el-breadcrumb-item>
        </transition-group>
      </el-breadcrumb>
    </div>
    <h3 class="currentNodeName">{{ name }}</h3>
  </div>
</template>

<script>
export default {
  data() {
    return {
      levelList: null,
      name:'',
    }
  },
  inject: ['reloadMain'],
  props: ['deptName','deptListName'],
  watch: {
    $route(route) {
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      const first = matched[0]
      if (!this.isDashboard(first)) {
        matched = [{ path: '/home', meta: { title: '社联首页' } }].concat(matched)
      }

      this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
      if (this.deptListName){
        if (this.levelList.length>=3){
          if (this.levelList[1].path=="/dept/index"){
            this.levelList[2].meta.title=this.deptListName
          }
        }
      }
      if (this.deptName){
        if (this.levelList.length>=4){
          if (this.levelList[2].path=="/dept/index/list"){
            this.levelList[3].meta.title=this.deptName
          }
        }
      }
    },
    isDashboard(route) {
      const name = route && route.name
      this.name=name;
      if (!name) {
        return false
      }
      return name.trim() === '社联首页'
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        if (redirect=="/dept/index"){
          sessionStorage.removeItem('deptListName')
        }
        this.$router.push(redirect)
        return
      }
      if (path=="/dept/index"){
        sessionStorage.removeItem('deptListName')
      }else if (path=="/post/list"){
        sessionStorage.removeItem('postId')
      }
      this.$router.push(path)
      this.reloadMain()
      // this.$router.go(0);
    }
  }
}
</script>

<style scoped>
/deep/.el-breadcrumb__inner {
  color: #333;
}
/deep/.el-breadcrumb__inner a, .el-breadcrumb__inner.is-link {
  font-weight: normal;
  color: #303133;
}
/deep/.el-breadcrumb__separator {
  margin: 0 4px;
  font-weight: 700;
}
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 55px;
  margin-left: 8px;
  height: 55px;
}
.app-breadcrumb.el-breadcrumb.no-redirect {
  color: #97a8be;
  cursor: text;
}
</style>
